package cn.bdqn.service.impl;

import cn.bdqn.domain.Registration;
import cn.bdqn.mapper.RegistrationMapper;
import cn.bdqn.service.RegistrationService;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 挂号表的业务实现
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class RegistrationServiceImpl implements RegistrationService {

    @Autowired
    private RegistrationMapper registrationMapper;
    @Resource
    private AmqpTemplate amqpTemplate;
    /**
     * 查询全部挂号信息为预约的信息
     * @return
     */
    @Override
    public List<Registration> queryAll() {
        return registrationMapper.selectAll();
    }
    /**
     *根据挂号名查询挂号信息
     * @param regisName
     * @return
     */
    @Override
    public List<Registration> queryByRegisName(String regisName) {
        return registrationMapper.selectByRegisName(regisName);
    }

    /**
     * 添加挂号信息时判断数据库是否存在重复数据
     * @param id
     * @return
     */
    @Override
    public  int queryVerifyById(String id) {
        return registrationMapper.selectVerifyById(id);
    }

    /**
     * 根据id修改挂号状态
     * @param id
     * @return
     */
    @Override
    @Transactional( rollbackFor =Exception.class ,propagation = Propagation.REQUIRES_NEW)
    public int updateById(String id) {
        registrationMapper.updateById(id);
        Registration registration=registrationMapper.selectById(id);
        amqpTemplate.convertAndSend("", "OPC-Queue", registration);
        return registrationMapper.updateById(id);
    }

    /**
     * 添加挂号信息
     * @param registration
     * @return
     */
    @Override
    @Transactional( rollbackFor =Exception.class ,propagation = Propagation.REQUIRES_NEW)
    public int addRegistration(Registration registration) {
        return registrationMapper.insert(registration);
    }

    /**
     * 根据id修改病人信息编号
     * @param id
     * @param menberId
     * @return
     */
    @Override
    @Transactional( rollbackFor =Exception.class ,propagation = Propagation.REQUIRES_NEW)
    public int updateMenberIdById(String  id,Integer menberId) {
        return registrationMapper.updateMenberIdById(id,menberId);
    }

    /**
     * 根据menberId修改支付状态
     * @param menberId
     * @return
     */
    @Override
    @Transactional( rollbackFor =Exception.class ,propagation = Propagation.REQUIRES_NEW)
    public int updateByMenberId(int menberId) {
        return registrationMapper.updateByMenberId(menberId);
    }

    @Override
    public Registration queryByMenberId(int menberId) {
        return registrationMapper.selectByMenberId(menberId);
    }

    /**
     * 根据phone指定查询挂号信息
     * @param phone
     * @return
     */
    @Override
    public Registration queryByPhone(String phone) {
        return registrationMapper.selectByPhone(phone);
    }

    /**
     * 根据id查询挂号信息
     * @param id
     * @return
     */
    @Override
    public Registration queryById(String id) {
        return registrationMapper.selectById(id);
    }


}
